package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.CfgDictionaryInfo;
import com.baitengsoft.patentapimonitor.api.model.WebExpiryNoticeInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantIndustryInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebExpiryNoticeInfoDto;
import com.baitengsoft.patentapimonitor.api.model.param.SearchExpiryNoticeParam;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 届满通知
 * @author Chen 2018/1/17
 */
@RestController
@RequestMapping("api/v1/web/expirynotice")
@Api(value = "前台-专利监控-届满通知",description = "届满通知")
public class WebExpiryNoticeController extends AbstractController {
    @Autowired
    private CfgDictionaryService cfgDictionaryService;
    @Autowired
    private PacPatentinfoService pacPatentinfoService;
    @Autowired
    private WebExcelExportService webExcelExportService;
    /**
     * 获取筛选参数
     */
    @ApiOperation(value ="获取查询参数-专利类型(patTypes)")
    @GetMapping("/type")
    @ResponseBody
    public Res getPatType(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        //获取专利类型和value
        List<CfgDictionaryInfo> patTypes = cfgDictionaryService.getNameAndValueByCode("1003");
        return Res.ok().put("patTypes",patTypes);
    }

    @ApiOperation(value ="获取列表")
    @GetMapping("/list")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "专利类型", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applyPersonLast", value = "专利人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getExpiryNoticeList(@ApiIgnore SearchExpiryNoticeParam param){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        PageResult<WebExpiryNoticeInfo> result = pacPatentinfoService.getExpiryNoticeList(userInfo.getPlatformid(), param);
        return Res.ok().put("list",result);
    }

    @ApiOperation(value ="列表导出")
    @GetMapping("/listexport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "专利类型", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applyPersonLast", value = "专利人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getExpiryNoticeListExport(@ApiIgnore SearchExpiryNoticeParam param,HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if (param.getPageSize() >5000)
            return Res.error("最多只可以导出5000条数据");
        PageResult<WebExpiryNoticeInfo> result = pacPatentinfoService.getExpiryNoticeList(userInfo.getPlatformid(), param);
        if (result == null || result.getItems() == null || result.getItems().size() < 1) {
            return Res.error("无数据");
        }
        List<WebExpiryNoticeInfo> items = result.getItems();
        List<WebExpiryNoticeInfoDto> webDtos = webExcelExportService.getWebExpiryNoticeInfoDto(items);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "届满通知详情.xls",
                "届满通知详情");
    }
}
